﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;

namespace DeepEarth.Core.Utilities
{
    /// <summary>
    /// Extra VisualTreeHelper type methods
    /// </summary>
    public static class VisualTreeHelperEx
    {
        public static IEnumerable<DependencyObject> Descendants(this DependencyObject o)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(o); i++)
            {
                var child = VisualTreeHelper.GetChild(o, i);
                yield return child;
                foreach (var child2 in child.Descendants())
                    yield return child2;
            }
        }

        public static IEnumerable<DependencyObject> Ancestors(this DependencyObject o)
        {
            var parent = VisualTreeHelper.GetParent(o);
            if (parent != null)
            {
                yield return parent;
                foreach (var grandparent in parent.Ancestors())
                    yield return grandparent;
            }
        }
    }
}